//
//  CVCellOfArticleCollect.m
//  LCOnePsychology
//
//  Created by li cheng on 16/9/21.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "CVCellOfArticleCollect.h"
#import "BaseTableView.h"
#import "MJMacro.h"
// 收藏文章的 tableViewCell
#import "TVCellOfReadAriticle.h"
#import "ZWYNetTool.h"
// 收藏文章的 model
#import "DataOfArticleCollect.h"
#import "FavoriteOfDataArticleCollect.h"
#import "RootClassOfArticleCollect.h"
#import "UserOfDataFavoriteArticleCollect.h"
// SDImageView
#import "UIImageView+WebCache.h"
// 跳转到的 VC
#import "VCOfReadWeb.h"
// 当前所在的 VC
#import "VCOfCollec.h"
@interface CVCellOfArticleCollect()<UITableViewDelegate, UITableViewDataSource>
// 收藏的文章
@property (nonatomic, strong) RootClassOfArticleCollect *rootClassOfArticleCollect;
// 收藏的测试
@property (nonatomic, strong) BaseTableView *tableViewOfCollect;
@end
@implementation CVCellOfArticleCollect
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
        [self handleArticleCollectData];
    }
    return self;
}
#pragma mark - 收藏文章,数据解析
- (void)handleArticleCollectData{
    [ZWYNetTool GET:@"http://yiapi.xinli001.com/v2/yi/my-newfavorite-list.json?key=18ee0593a01c07f71d28fa936d671457&_platform=ios&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93&type=article" andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        NSLog(@"%@", result);
        _rootClassOfArticleCollect = [RootClassOfArticleCollect modelDic:result];
        [self.tableViewOfCollect reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}
#pragma mark - 创建 tableView
- (void)createTableView{
    self.tableViewOfCollect = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    [self.contentView addSubview:_tableViewOfCollect];
    self.tableViewOfCollect.delegate = self;
    self.tableViewOfCollect.dataSource = self;
    [self.tableViewOfCollect registerNib:[UINib nibWithNibName:@"TVCellOfReadAriticle" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TVCellOfReadAriticle"];
}
#pragma mark - tabelView 代理方法 row 的数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _rootClassOfArticleCollect.data.favorite.count;
}
#pragma mark - 返回每个 row 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return SCREEN_HEIGHT / 6;
}
#pragma mark - 分区头的文字
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString *string = [NSString stringWithFormat:@"  一共%@篇", _rootClassOfArticleCollect.data.num];
    return string;
}
#pragma mark - TableView 代理方法 cell 的赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TVCellOfReadAriticle *cell = [tableView dequeueReusableCellWithIdentifier:@"TVCellOfReadAriticle" forIndexPath:indexPath];
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:_rootClassOfArticleCollect.data.favorite[indexPath.row].cover] placeholderImage:[UIImage imageNamed:@"0"]];
    cell.titleLabel.text = _rootClassOfArticleCollect.data.favorite[indexPath.row].title;
    // 这里不是作者了, 是时间
    cell.authorLabel.text = _rootClassOfArticleCollect.data.favorite[indexPath.row].created;
    cell.viewnumLabel.text = _rootClassOfArticleCollect.data.favorite[indexPath.row].viewnum;
    return cell;
}
#pragma mark - tableView 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    VCOfReadWeb *vcOfReadWeb = [[VCOfReadWeb alloc] init];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vcOfReadWeb];
    NSLog(@"%@", self.nextResponder.nextResponder.nextResponder);
    VCOfCollec *vcOfCollec = (VCOfCollec *)self.nextResponder.nextResponder.nextResponder;
    vcOfReadWeb.stringURL = [NSString stringWithFormat:@"http://yiapi.xinli001.com/v2/yi/article-detail-newpage/%@?user_id=1004358517&_version=4.0", _rootClassOfArticleCollect.data.favorite[indexPath.row].ID];
    vcOfReadWeb.myID = _rootClassOfArticleCollect.data.favorite[indexPath.row].ID;
    [vcOfCollec presentViewController:navi animated:YES completion:^{
        
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
